#!/usr/bin/env python
# -*- coding: utf-8 -*-


# 文件名：iCLI.py
# 版本：Ver1.0.0
# 作者：Christopher PAI
# 使用方法：参考程序入口（Main）


DEBUG = 0


class Do_go(object):
    Help = """
    go    - Go command
    """
    HelpSelf = """
    go command help
    """

    def __init__(self, args=[]):
        self.parameter = args
        self._para_len = 0
        if DEBUG == 1:
            print(self.parameter)

    def do(self):
        if len(self.parameter) == 0:
            print("Go Go Go!!!")
        else:
            if 'a_on' in self.parameter:
                self._para_len = 1
                print("a is on")
            if "b_on" in self.parameter:
                self._para_len = 1
                print("b is on")


class Do_quit(object):
    Help = """
    quit  - Quit program
    """
    HelpSelf = """
    quit [QUIT CODE]
    """

    def __init__(self, args=[]):
        self.parameter = args
        if DEBUG == 1:
            print(self.parameter)

    def do(self):
        if len(self.parameter) == 0:
            quit()
        else:
            quit(self.parameter[0])


class Do_help(object):
    Help = """
    help  - Help information
    """
    HelpSelf = """
    help [COMMAND]
    help usage   - Print iCLI usage
    """
    Usage = '''
    说明：类Ice_CLI、Do_help、Do_quit无特殊情况不用改动
    增加命令：
        可参考Do_help、Do_quit，类名称Do_后面的字符串就是命令名，示例如下：
            class Do_COMMANDNAME(object):
                Help = """
                命令列表中的帮助信息
                """
                HelpSelf = """
                单独查询命令用法的帮助信息
                """
                Usage = """
                类Do_help特有属性，用来描述整个iCLI.py如何使用
                """
            
                def __init__(self, args=[]):
                    self.parameter = args
                    if DEBUG == 1:
                        print(self.parameter)
            
                def do(self):
                    if len(self.parameter) == 0:
                        无参数命令的操作
                    else:
                        有参数命令的操作
    '''

    def __init__(self, args=[]):
        self.parameter = args
        if DEBUG == 1:
            print("Class help __INIT__   :", end="")
            print(self.parameter)

    def do(self):
        if len(self.parameter) == 0:
            print("Help information:\n")
            for i in globals().keys():
                if DEBUG == 1:
                    print(i)
                if "Do_" in i:
                    print(globals()[i].Help)
            print()
        elif self.parameter[0] == "usage":
            print(self.Usage)
        else:
            print("Help information:\n")
            print(globals()["Do_" + self.parameter[0]]().HelpSelf)
            print()


class Ice_CLI(object):
    def __init__(self):
        self.prompt = "Ice CLI-{}> ".format("1.0.0")

    def run(self):
        while True:
            comm = input(self.prompt).split(" ")
            if DEBUG == 1:
                print(comm[0])
                print(comm[1:])
                print("Do_" + comm[0])
            try:
                go = globals()["Do_" + comm[0]](comm[1:])
                go.do()
            except KeyError:
                print("Not found command: '{}' !!!\n".format(comm[0]))
                continue


if __name__ == "__main__":
    CLI = Ice_CLI()
    CLI.run()
